import time
t1=time.time()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC, select, wait
from selenium.webdriver.common.by import By
import re
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
driver=webdriver.Chrome('K:/zhusc/chromedriver.exe', options=chrome_options)
with open('./stealth.min.js') as f:
    js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
driver.get("https://spa8.scrape.center/")
def wait_for_you(myDynamicElement):
    try:
        wait=WebDriverWait(driver,20)
        element=wait.until(EC.presence_of_element_located((By.XPATH,myDynamicElement)))
    except:
        print('can not find the element:',myDynamicElement)
driver.maximize_window() #窗口最大化
wait_for_you('//*[@id="players"]/div/div/div[1]/div/div/div/div[2]/h3')
html=driver.page_source
pattern=r"(?<=name:')(.+?)(?=',)"
name_list=re.findall(pattern,html)
print(name_list)
driver.quit()
print(time.time()-t1)#约一分钟
